{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5678b63a-3352-4256-8691-d9578a292fa9",
   "metadata": {},
   "source": [
    "## Estimate standard errors for structural parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "56a0e031-ed12-4bb1-9820-7183d7a4c0e4",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "from fn_simulate import *\n",
    "\n",
    "np.set_printoptions(precision=4)\n",
    "\n",
    "# randomization variables for simulation\n",
    "#import secrets\n",
    "#secrets.randbits(128) \n",
    "seed = 84708628852577542122415832887111152745\n",
    "\n",
    "rng = np.random.default_rng(seed)\n",
    "\n",
    "n_draws = 1000\n",
    "\n",
    "#param_random = {'n_draws' : n_draws, 'seed' : seed}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ca330fd5-a3cf-4d09-a8af-b0820508d2f9",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# calibrated parameters\n",
    "\n",
    "    # income\n",
    "mu_lny = 9.713\n",
    "sig_lny = 0.497\n",
    "\n",
    "mu_eps = 0\n",
    "\n",
    "    # prices\n",
    "pi_s = 1\n",
    "pi_q = 0\n",
    "tau = 0.075\n",
    "\n",
    "gamma = 0.424\n",
    "\n",
    "param_given = {'mu_lny' : mu_lny, \n",
    "               'sig_lny' : sig_lny,\n",
    "               'mu_eps': mu_eps,\n",
    "               'pi_s' : pi_s, \n",
    "               'pi_q' : pi_q, \n",
    "               'tau' : tau,\n",
    "               'gamma': gamma}\n",
    "\n",
    "# estimated parameters\n",
    "# initial: [ 1.467219  0.110516  0.92719   0.611335 -7.924372  0.299971 -0.139612 -0.010366]\n",
    "# results:  [ 1.466967  0.110715  0.927835  0.612176 -7.919992  0.299912 -0.140181 -0.009912]\n",
    "\n",
    "# results:\n",
    "pi_n = 1.466967\n",
    "pi_nq = 0.110715\n",
    "\n",
    "theta = 0.927835\n",
    "alpha = 0.612176\n",
    "rho = -7.919992\n",
    "\n",
    "sig_eps = 0.299912\n",
    "eps_cutoff = -0.140181\n",
    "\n",
    "cor_lny_eps = -0.009912\n",
    "\n",
    "\n",
    "param_choice = np.array( \n",
    "    [pi_n, pi_nq, \n",
    "     theta, alpha, rho,\n",
    "     sig_eps, eps_cutoff, cor_lny_eps] \n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bccf8ec9-4482-4e72-8528-7f09b1a5f5e3",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# moments in the real data\n",
    "\n",
    "# moments observed in real data\n",
    "beta_A_2to3_real = -0.157  \n",
    "beta_B_2to3_real = 0.412 \n",
    "\n",
    "PA_2to3_real = 0.212\n",
    "PB_2to3_real = 0.121\n",
    "\n",
    "exp_n_AB_real = 0.214\n",
    "exp_nq_AB_real = 0.111\n",
    "\n",
    "cor_lny_n3_real = 0.128\n",
    "cor_lny_lnq_AB_real = 0.296\n",
    "\n",
    "moments_real = np.array( \n",
    "    [beta_A_2to3_real, beta_B_2to3_real, PA_2to3_real, PB_2to3_real, \n",
    "     exp_n_AB_real, exp_nq_AB_real, cor_lny_n3_real, cor_lny_lnq_AB_real] \n",
    ")\n",
    "\n",
    "# weights: 1/moments_real^2 as in Baudin, de la Croix, & Gobbi (2015, AER)\n",
    "W = np.diag( 1 / np.square(moments_real) )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b3700f03-eb1a-4064-9a76-8c26e8d60a7d",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "mu = [mu_lny, mu_eps]\n",
    "cov_lny_eps = cor_lny_eps * sig_lny * sig_eps\n",
    "\n",
    "cov = [[sig_lny**2,cov_lny_eps],[cov_lny_eps,sig_eps**2]]\n",
    "\n",
    "    # draw from a joint-normal distribution of log(income) and eps\n",
    "rng = np.random.default_rng(seed) # use a pre-specified seed for replication\n",
    "\n",
    "lny_eps_draws = rng.multivariate_normal(mu, cov, n_draws)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "58994202-2e12-410d-90a4-b0f984f104dd",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 9.8786e+00, -9.4598e-03],\n",
       "       [ 9.5093e+00, -1.1925e-01],\n",
       "       [ 1.0209e+01,  1.0015e-01],\n",
       "       ...,\n",
       "       [ 9.8851e+00, -4.4118e-01],\n",
       "       [ 8.8455e+00, -3.0308e-01],\n",
       "       [ 9.3522e+00,  4.9110e-02]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lny_eps_draws"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "300b129c-dc84-4867-ad3a-2538017736b5",
   "metadata": {
    "collapsed": true,
    "editable": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "scrolled": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# iteration:  0\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.013288\n",
      "         Iterations: 216\n",
      "         Function evaluations: 412\n",
      "--- 1685.2331762313843 seconds ---\n",
      "# iteration:  1\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.121312\n",
      "         Iterations: 204\n",
      "         Function evaluations: 399\n",
      "--- 1783.4425194263458 seconds ---\n",
      "# iteration:  2\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.031859\n",
      "         Iterations: 173\n",
      "         Function evaluations: 341\n",
      "--- 1473.9865565299988 seconds ---\n",
      "# iteration:  3\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.022059\n",
      "         Iterations: 421\n",
      "         Function evaluations: 749\n",
      "--- 3110.5468316078186 seconds ---\n",
      "# iteration:  4\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.023081\n",
      "         Iterations: 626\n",
      "         Function evaluations: 1055\n",
      "--- 8170.611980676651 seconds ---\n",
      "# iteration:  5\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.006919\n",
      "         Iterations: 208\n",
      "         Function evaluations: 402\n",
      "--- 1637.7517647743225 seconds ---\n",
      "# iteration:  6\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.058413\n",
      "         Iterations: 126\n",
      "         Function evaluations: 272\n",
      "--- 1139.6665678024292 seconds ---\n",
      "# iteration:  7\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.025532\n",
      "         Iterations: 143\n",
      "         Function evaluations: 308\n",
      "--- 1304.2331638336182 seconds ---\n",
      "# iteration:  8\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.062930\n",
      "         Iterations: 419\n",
      "         Function evaluations: 711\n",
      "--- 2920.6072006225586 seconds ---\n",
      "# iteration:  9\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.034009\n",
      "         Iterations: 226\n",
      "         Function evaluations: 422\n",
      "--- 1482.7802064418793 seconds ---\n",
      "# iteration:  10\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.027815\n",
      "         Iterations: 215\n",
      "         Function evaluations: 417\n",
      "--- 1755.2935199737549 seconds ---\n",
      "# iteration:  11\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.089315\n",
      "         Iterations: 229\n",
      "         Function evaluations: 432\n",
      "--- 1712.2970976829529 seconds ---\n",
      "# iteration:  12\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.031463\n",
      "         Iterations: 335\n",
      "         Function evaluations: 608\n",
      "--- 2530.093828201294 seconds ---\n",
      "# iteration:  13\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.042019\n",
      "         Iterations: 125\n",
      "         Function evaluations: 271\n",
      "--- 1140.2372651100159 seconds ---\n",
      "# iteration:  14\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.044509\n",
      "         Iterations: 202\n",
      "         Function evaluations: 371\n",
      "--- 1526.1917593479156 seconds ---\n",
      "# iteration:  15\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.048458\n",
      "         Iterations: 301\n",
      "         Function evaluations: 559\n",
      "--- 2394.4603476524353 seconds ---\n",
      "# iteration:  16\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.026180\n",
      "         Iterations: 157\n",
      "         Function evaluations: 337\n",
      "--- 1442.5737855434418 seconds ---\n",
      "# iteration:  17\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.044951\n",
      "         Iterations: 234\n",
      "         Function evaluations: 455\n",
      "--- 1929.6943092346191 seconds ---\n",
      "# iteration:  18\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.018901\n",
      "         Iterations: 402\n",
      "         Function evaluations: 698\n",
      "--- 2882.269884109497 seconds ---\n",
      "# iteration:  19\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.007954\n",
      "         Iterations: 271\n",
      "         Function evaluations: 491\n",
      "--- 2052.745890378952 seconds ---\n",
      "# iteration:  20\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.007151\n",
      "         Iterations: 149\n",
      "         Function evaluations: 326\n",
      "--- 1368.4647963047028 seconds ---\n",
      "# iteration:  21\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.025097\n",
      "         Iterations: 132\n",
      "         Function evaluations: 281\n",
      "--- 1182.0658740997314 seconds ---\n",
      "# iteration:  22\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.068146\n",
      "         Iterations: 305\n",
      "         Function evaluations: 559\n",
      "--- 2508.6597039699554 seconds ---\n",
      "# iteration:  23\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.017288\n",
      "         Iterations: 173\n",
      "         Function evaluations: 328\n",
      "--- 1365.550570011139 seconds ---\n",
      "# iteration:  24\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.043489\n",
      "         Iterations: 278\n",
      "         Function evaluations: 505\n",
      "--- 2159.2253198623657 seconds ---\n",
      "# iteration:  25\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.027589\n",
      "         Iterations: 428\n",
      "         Function evaluations: 762\n",
      "--- 3117.024146795273 seconds ---\n",
      "# iteration:  26\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.163365\n",
      "         Iterations: 163\n",
      "         Function evaluations: 347\n",
      "--- 1420.6219420433044 seconds ---\n",
      "# iteration:  27\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.082504\n",
      "         Iterations: 191\n",
      "         Function evaluations: 394\n",
      "--- 1695.6838586330414 seconds ---\n",
      "# iteration:  28\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.011008\n",
      "         Iterations: 248\n",
      "         Function evaluations: 465\n",
      "--- 2026.4411296844482 seconds ---\n",
      "# iteration:  29\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.033011\n",
      "         Iterations: 277\n",
      "         Function evaluations: 525\n",
      "--- 2190.3206243515015 seconds ---\n",
      "# iteration:  30\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.063410\n",
      "         Iterations: 334\n",
      "         Function evaluations: 626\n",
      "--- 2577.504246711731 seconds ---\n",
      "# iteration:  31\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.149311\n",
      "         Iterations: 315\n",
      "         Function evaluations: 569\n",
      "--- 2395.375300168991 seconds ---\n",
      "# iteration:  32\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.043616\n",
      "         Iterations: 360\n",
      "         Function evaluations: 646\n",
      "--- 2797.1761360168457 seconds ---\n",
      "# iteration:  33\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.040813\n",
      "         Iterations: 396\n",
      "         Function evaluations: 681\n",
      "--- 2788.401966571808 seconds ---\n",
      "# iteration:  34\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.204001\n",
      "         Iterations: 406\n",
      "         Function evaluations: 721\n",
      "--- 3034.2607605457306 seconds ---\n",
      "# iteration:  35\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.015436\n",
      "         Iterations: 249\n",
      "         Function evaluations: 480\n",
      "--- 2090.1029484272003 seconds ---\n",
      "# iteration:  36\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.052655\n",
      "         Iterations: 346\n",
      "         Function evaluations: 614\n",
      "--- 2567.5036358833313 seconds ---\n",
      "# iteration:  37\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.047813\n",
      "         Iterations: 385\n",
      "         Function evaluations: 669\n",
      "--- 2631.869149208069 seconds ---\n",
      "# iteration:  38\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.008755\n",
      "         Iterations: 326\n",
      "         Function evaluations: 575\n",
      "--- 2375.657234430313 seconds ---\n",
      "# iteration:  39\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.105235\n",
      "         Iterations: 247\n",
      "         Function evaluations: 450\n",
      "--- 1734.651379108429 seconds ---\n",
      "# iteration:  40\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.022859\n",
      "         Iterations: 189\n",
      "         Function evaluations: 352\n",
      "--- 1474.7852308750153 seconds ---\n",
      "# iteration:  41\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.015942\n",
      "         Iterations: 188\n",
      "         Function evaluations: 382\n",
      "--- 1608.6237254142761 seconds ---\n",
      "# iteration:  42\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.044665\n",
      "         Iterations: 326\n",
      "         Function evaluations: 596\n",
      "--- 2481.173485517502 seconds ---\n",
      "# iteration:  43\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.002713\n",
      "         Iterations: 155\n",
      "         Function evaluations: 323\n",
      "--- 1374.1835005283356 seconds ---\n",
      "# iteration:  44\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.012262\n",
      "         Iterations: 258\n",
      "         Function evaluations: 483\n",
      "--- 2077.9459388256073 seconds ---\n",
      "# iteration:  45\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.009543\n",
      "         Iterations: 315\n",
      "         Function evaluations: 553\n",
      "--- 2344.6986458301544 seconds ---\n",
      "# iteration:  46\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.012326\n",
      "         Iterations: 150\n",
      "         Function evaluations: 319\n",
      "--- 1364.339194059372 seconds ---\n",
      "# iteration:  47\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.012001\n",
      "         Iterations: 186\n",
      "         Function evaluations: 360\n",
      "--- 1532.8281152248383 seconds ---\n",
      "# iteration:  48\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.061320\n",
      "         Iterations: 204\n",
      "         Function evaluations: 423\n",
      "--- 1846.6529643535614 seconds ---\n",
      "# iteration:  49\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.009954\n",
      "         Iterations: 119\n",
      "         Function evaluations: 274\n",
      "--- 1169.8926084041595 seconds ---\n",
      "# iteration:  50\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.036850\n",
      "         Iterations: 279\n",
      "         Function evaluations: 509\n",
      "--- 2241.4181389808655 seconds ---\n",
      "# iteration:  51\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.509485\n",
      "         Iterations: 194\n",
      "         Function evaluations: 383\n",
      "--- 1629.4127340316772 seconds ---\n",
      "# iteration:  52\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.003827\n",
      "         Iterations: 206\n",
      "         Function evaluations: 395\n",
      "--- 1666.021241426468 seconds ---\n",
      "# iteration:  53\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.020516\n",
      "         Iterations: 376\n",
      "         Function evaluations: 680\n",
      "--- 2795.50674366951 seconds ---\n",
      "# iteration:  54\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.031543\n",
      "         Iterations: 201\n",
      "         Function evaluations: 368\n",
      "--- 1647.1269006729126 seconds ---\n",
      "# iteration:  55\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.062941\n",
      "         Iterations: 423\n",
      "         Function evaluations: 735\n",
      "--- 3223.0054399967194 seconds ---\n",
      "# iteration:  56\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.046066\n",
      "         Iterations: 192\n",
      "         Function evaluations: 366\n",
      "--- 1637.6476106643677 seconds ---\n",
      "# iteration:  57\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.073140\n",
      "         Iterations: 397\n",
      "         Function evaluations: 696\n",
      "--- 2718.5303933620453 seconds ---\n",
      "# iteration:  58\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.058522\n",
      "         Iterations: 405\n",
      "         Function evaluations: 760\n",
      "--- 3400.7257103919983 seconds ---\n",
      "# iteration:  59\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.044644\n",
      "         Iterations: 227\n",
      "         Function evaluations: 431\n",
      "--- 1891.380788564682 seconds ---\n",
      "# iteration:  60\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.022733\n",
      "         Iterations: 458\n",
      "         Function evaluations: 803\n",
      "--- 3808.1386103630066 seconds ---\n",
      "# iteration:  61\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.056895\n",
      "         Iterations: 237\n",
      "         Function evaluations: 441\n",
      "--- 2070.7892940044403 seconds ---\n",
      "# iteration:  62\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.040632\n",
      "         Iterations: 131\n",
      "         Function evaluations: 296\n",
      "--- 1406.4971148967743 seconds ---\n",
      "# iteration:  63\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.050417\n",
      "         Iterations: 504\n",
      "         Function evaluations: 871\n",
      "--- 4030.509744167328 seconds ---\n",
      "# iteration:  64\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.038769\n",
      "         Iterations: 173\n",
      "         Function evaluations: 339\n",
      "--- 1573.3194692134857 seconds ---\n",
      "# iteration:  65\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.030613\n",
      "         Iterations: 256\n",
      "         Function evaluations: 470\n",
      "--- 2177.7490911483765 seconds ---\n",
      "# iteration:  66\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.040629\n",
      "         Iterations: 173\n",
      "         Function evaluations: 342\n",
      "--- 1635.4880168437958 seconds ---\n",
      "# iteration:  67\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.002938\n",
      "         Iterations: 271\n",
      "         Function evaluations: 520\n",
      "--- 2396.9854822158813 seconds ---\n",
      "# iteration:  68\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.036376\n",
      "         Iterations: 429\n",
      "         Function evaluations: 740\n",
      "--- 4001.391330718994 seconds ---\n",
      "# iteration:  69\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.054765\n",
      "         Iterations: 165\n",
      "         Function evaluations: 339\n",
      "--- 1999.4102804660797 seconds ---\n",
      "# iteration:  70\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.017863\n",
      "         Iterations: 267\n",
      "         Function evaluations: 506\n",
      "--- 3081.3652589321136 seconds ---\n",
      "# iteration:  71\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.051365\n",
      "         Iterations: 262\n",
      "         Function evaluations: 470\n",
      "--- 2860.8338623046875 seconds ---\n",
      "# iteration:  72\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.002335\n",
      "         Iterations: 205\n",
      "         Function evaluations: 393\n",
      "--- 2330.9332835674286 seconds ---\n",
      "# iteration:  73\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.010844\n",
      "         Iterations: 272\n",
      "         Function evaluations: 514\n",
      "--- 3173.1688826084137 seconds ---\n",
      "# iteration:  74\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.040864\n",
      "         Iterations: 337\n",
      "         Function evaluations: 626\n",
      "--- 3772.4636166095734 seconds ---\n",
      "# iteration:  75\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.061521\n",
      "         Iterations: 239\n",
      "         Function evaluations: 463\n",
      "--- 2922.678556919098 seconds ---\n",
      "# iteration:  76\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.011052\n",
      "         Iterations: 216\n",
      "         Function evaluations: 404\n",
      "--- 2500.2706763744354 seconds ---\n",
      "# iteration:  77\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.065939\n",
      "         Iterations: 249\n",
      "         Function evaluations: 456\n",
      "--- 3177.8700149059296 seconds ---\n",
      "# iteration:  78\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.101863\n",
      "         Iterations: 252\n",
      "         Function evaluations: 460\n",
      "--- 3476.73974609375 seconds ---\n",
      "# iteration:  79\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.021303\n",
      "         Iterations: 160\n",
      "         Function evaluations: 323\n",
      "--- 2430.3769330978394 seconds ---\n",
      "# iteration:  80\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.046730\n",
      "         Iterations: 396\n",
      "         Function evaluations: 694\n",
      "--- 5069.245744943619 seconds ---\n",
      "# iteration:  81\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.022781\n",
      "         Iterations: 501\n",
      "         Function evaluations: 870\n",
      "--- 6508.64582824707 seconds ---\n",
      "# iteration:  82\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.003970\n",
      "         Iterations: 305\n",
      "         Function evaluations: 553\n",
      "--- 3059.0634503364563 seconds ---\n",
      "# iteration:  83\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.053657\n",
      "         Iterations: 360\n",
      "         Function evaluations: 635\n",
      "--- 2507.4091386795044 seconds ---\n",
      "# iteration:  84\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.119023\n",
      "         Iterations: 462\n",
      "         Function evaluations: 1119\n",
      "--- 7418.380155801773 seconds ---\n",
      "# iteration:  85\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.016841\n",
      "         Iterations: 282\n",
      "         Function evaluations: 533\n",
      "--- 2140.863624572754 seconds ---\n",
      "# iteration:  86\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.018181\n",
      "         Iterations: 220\n",
      "         Function evaluations: 412\n",
      "--- 1802.1404736042023 seconds ---\n",
      "# iteration:  87\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.087686\n",
      "         Iterations: 319\n",
      "         Function evaluations: 571\n",
      "--- 2459.8886885643005 seconds ---\n",
      "# iteration:  88\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.031697\n",
      "         Iterations: 223\n",
      "         Function evaluations: 439\n",
      "--- 1875.976218700409 seconds ---\n",
      "# iteration:  89\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.016625\n",
      "         Iterations: 420\n",
      "         Function evaluations: 758\n",
      "--- 3344.3647627830505 seconds ---\n",
      "# iteration:  90\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.013989\n",
      "         Iterations: 336\n",
      "         Function evaluations: 594\n",
      "--- 2598.194284915924 seconds ---\n",
      "# iteration:  91\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.012717\n",
      "         Iterations: 215\n",
      "         Function evaluations: 432\n",
      "--- 1839.3806397914886 seconds ---\n",
      "# iteration:  92\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.007077\n",
      "         Iterations: 374\n",
      "         Function evaluations: 684\n",
      "--- 2858.9543232917786 seconds ---\n",
      "# iteration:  93\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.011866\n",
      "         Iterations: 209\n",
      "         Function evaluations: 386\n",
      "--- 1643.8840370178223 seconds ---\n",
      "# iteration:  94\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.014097\n",
      "         Iterations: 483\n",
      "         Function evaluations: 861\n",
      "--- 3674.547703027725 seconds ---\n",
      "# iteration:  95\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.020603\n",
      "         Iterations: 283\n",
      "         Function evaluations: 528\n",
      "--- 2218.057872056961 seconds ---\n",
      "# iteration:  96\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.026368\n",
      "         Iterations: 221\n",
      "         Function evaluations: 403\n",
      "--- 1762.2866899967194 seconds ---\n",
      "# iteration:  97\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.005811\n",
      "         Iterations: 241\n",
      "         Function evaluations: 431\n",
      "--- 1748.7693650722504 seconds ---\n",
      "# iteration:  98\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.065194\n",
      "         Iterations: 139\n",
      "         Function evaluations: 301\n",
      "--- 1113.618623971939 seconds ---\n",
      "# iteration:  99\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.232592\n",
      "         Iterations: 390\n",
      "         Function evaluations: 690\n",
      "--- 2872.822297811508 seconds ---\n"
     ]
    }
   ],
   "source": [
    "# Fix the initial guess for the choice parameters\n",
    "param_choice0 = param_choice\n",
    "\n",
    "param_choice_list = []\n",
    "\n",
    "for i in range(100):\n",
    "    \n",
    "    print(\"# iteration: \", i)\n",
    "    start_time = time.time()\n",
    "    \n",
    "    # draw\n",
    "    index = rng.choice(n_draws, n_draws, replace = True)\n",
    "    \n",
    "    # Display for each simulation: param_choice and value of the objective function\n",
    "    disp = False\n",
    "\n",
    "    # Define the objective function to be minimized\n",
    "    def objective_function(param_choice): # choice variables should be a numpy array\n",
    "\n",
    "        return simulate_objective_se(simulate_moments_se, param_choice, \n",
    "                                     param_given, lny_eps_draws[index], moments_real, W, disp = disp)\n",
    "\n",
    "    \n",
    "    # Perform the optimization\n",
    "    result = minimize(objective_function, param_choice0, method = 'Nelder-Mead', \n",
    "                      options = {'disp':True, 'fatol':0.0001, 'maxiter':1000}\n",
    "                     )\n",
    "    \n",
    "    # store results\n",
    "    param_choice_list.append(result.x)\n",
    "    \n",
    "    print(\"--- %s seconds ---\" % (time.time() - start_time))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "54832101-abe8-41dc-92dd-b21a4acad565",
   "metadata": {},
   "outputs": [],
   "source": [
    "param_names = ['pi_n', 'pi_nq', 'theta', 'alpha', 'rho', 'sig_eps', 'eps_cutoff', 'cor_lny_eps']\n",
    "\n",
    "df = pd.DataFrame(param_choice_list, columns = param_names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e5fae3f7-65b9-499f-be6a-2ff5400b436b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pi_n           0.036654\n",
       "pi_nq          0.002801\n",
       "theta          0.018744\n",
       "alpha          0.018468\n",
       "rho            0.285873\n",
       "sig_eps        0.012174\n",
       "eps_cutoff     0.004461\n",
       "cor_lny_eps    0.000425\n",
       "dtype: float64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.std()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2ff7c2a7-2c37-41fe-8ae9-36dd6c856014",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv('./df_bootstrap100.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d0661161-a019-4d0b-96a3-443cd1c3c660",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pi_n</th>\n",
       "      <th>pi_nq</th>\n",
       "      <th>theta</th>\n",
       "      <th>alpha</th>\n",
       "      <th>rho</th>\n",
       "      <th>sig_eps</th>\n",
       "      <th>eps_cutoff</th>\n",
       "      <th>cor_lny_eps</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.467314</td>\n",
       "      <td>0.110222</td>\n",
       "      <td>0.938380</td>\n",
       "      <td>0.618429</td>\n",
       "      <td>-8.156617</td>\n",
       "      <td>0.299118</td>\n",
       "      <td>-0.140523</td>\n",
       "      <td>-0.009920</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.333709</td>\n",
       "      <td>0.111429</td>\n",
       "      <td>0.945473</td>\n",
       "      <td>0.627962</td>\n",
       "      <td>-8.036161</td>\n",
       "      <td>0.297018</td>\n",
       "      <td>-0.140437</td>\n",
       "      <td>-0.010266</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.395903</td>\n",
       "      <td>0.108759</td>\n",
       "      <td>0.930731</td>\n",
       "      <td>0.628460</td>\n",
       "      <td>-8.148015</td>\n",
       "      <td>0.299826</td>\n",
       "      <td>-0.139550</td>\n",
       "      <td>-0.010106</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.467272</td>\n",
       "      <td>0.108311</td>\n",
       "      <td>0.938041</td>\n",
       "      <td>0.622577</td>\n",
       "      <td>-8.418641</td>\n",
       "      <td>0.305280</td>\n",
       "      <td>-0.139144</td>\n",
       "      <td>-0.010676</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.422294</td>\n",
       "      <td>0.112590</td>\n",
       "      <td>0.912583</td>\n",
       "      <td>0.638143</td>\n",
       "      <td>-7.619310</td>\n",
       "      <td>0.355108</td>\n",
       "      <td>-0.143482</td>\n",
       "      <td>-0.008403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>1.463206</td>\n",
       "      <td>0.109385</td>\n",
       "      <td>0.925026</td>\n",
       "      <td>0.620852</td>\n",
       "      <td>-8.456877</td>\n",
       "      <td>0.292350</td>\n",
       "      <td>-0.140588</td>\n",
       "      <td>-0.010001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>1.470223</td>\n",
       "      <td>0.111136</td>\n",
       "      <td>0.951698</td>\n",
       "      <td>0.612834</td>\n",
       "      <td>-7.961398</td>\n",
       "      <td>0.300462</td>\n",
       "      <td>-0.140436</td>\n",
       "      <td>-0.009926</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>1.485416</td>\n",
       "      <td>0.113334</td>\n",
       "      <td>0.928421</td>\n",
       "      <td>0.630213</td>\n",
       "      <td>-8.063449</td>\n",
       "      <td>0.298457</td>\n",
       "      <td>-0.141226</td>\n",
       "      <td>-0.008962</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>1.494860</td>\n",
       "      <td>0.112709</td>\n",
       "      <td>0.907534</td>\n",
       "      <td>0.585909</td>\n",
       "      <td>-7.861172</td>\n",
       "      <td>0.313587</td>\n",
       "      <td>-0.139958</td>\n",
       "      <td>-0.009832</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>1.506943</td>\n",
       "      <td>0.111357</td>\n",
       "      <td>0.930855</td>\n",
       "      <td>0.628500</td>\n",
       "      <td>-8.111846</td>\n",
       "      <td>0.302672</td>\n",
       "      <td>-0.137591</td>\n",
       "      <td>-0.009948</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        pi_n     pi_nq     theta     alpha       rho   sig_eps  eps_cutoff  \\\n",
       "0   1.467314  0.110222  0.938380  0.618429 -8.156617  0.299118   -0.140523   \n",
       "1   1.333709  0.111429  0.945473  0.627962 -8.036161  0.297018   -0.140437   \n",
       "2   1.395903  0.108759  0.930731  0.628460 -8.148015  0.299826   -0.139550   \n",
       "3   1.467272  0.108311  0.938041  0.622577 -8.418641  0.305280   -0.139144   \n",
       "4   1.422294  0.112590  0.912583  0.638143 -7.619310  0.355108   -0.143482   \n",
       "..       ...       ...       ...       ...       ...       ...         ...   \n",
       "95  1.463206  0.109385  0.925026  0.620852 -8.456877  0.292350   -0.140588   \n",
       "96  1.470223  0.111136  0.951698  0.612834 -7.961398  0.300462   -0.140436   \n",
       "97  1.485416  0.113334  0.928421  0.630213 -8.063449  0.298457   -0.141226   \n",
       "98  1.494860  0.112709  0.907534  0.585909 -7.861172  0.313587   -0.139958   \n",
       "99  1.506943  0.111357  0.930855  0.628500 -8.111846  0.302672   -0.137591   \n",
       "\n",
       "    cor_lny_eps  \n",
       "0     -0.009920  \n",
       "1     -0.010266  \n",
       "2     -0.010106  \n",
       "3     -0.010676  \n",
       "4     -0.008403  \n",
       "..          ...  \n",
       "95    -0.010001  \n",
       "96    -0.009926  \n",
       "97    -0.008962  \n",
       "98    -0.009832  \n",
       "99    -0.009948  \n",
       "\n",
       "[100 rows x 8 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b625d857-5943-4d3a-a408-295ed29e288c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
